
import type { PlasmoCSConfig } from "plasmo"
import { getDomData } from "./utils/getDom";

// 确保配置正确
export const config: PlasmoCSConfig = {
    matches: ["<all_urls>"],
    run_at: "document_end",
    all_frames: false
}

// 添加调试信息
console.log("🚀 Plasmo Content Script 已加载", {
    url: window.location.href,
    timestamp: new Date().toISOString(),
    // @ts-ignore
    chromeRuntime: !!chrome.runtime
})

const LogicOnlyContentScript = () => {

    console.log('LogicOnlyContentScript start');
    

    // 监听来自 Popup 的消息
    // @ts-ignore
    chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
        console.log("Content Script 收到消息:", request, "来自:", sender)

        if (request.action === "getDomData") {
            // 获取页面节点信息
            const result= getDomData()

            const response = {
                status: "success",
                data: result,
                pageInfo: {
                    title: document.title,
                    url: window.location.href,
                    timestamp: new Date().toISOString()
                }
            }

            

            sendResponse(response)

            // // 主动发送消息到 Popup
            // // @ts-ignore
            // chrome.runtime.sendMessage({
            //     action: "contentToPopup",
            //     data: "这是来自 Content Script 的主动消息"
            // }).catch(error => {
            //     console.log("发送到 Popup 失败:", error)
            // })
        }

        return true
    })













    return null; // 不渲染任何UI
};
export default LogicOnlyContentScript;